<template>
    <div class="selectpp-auditor">
        <div style="text-align:center;margin-bottom:10px">
            <RadioGroup v-model="partype" type="button" size="small" @on-change="ptchange">
                <Radio label="dus">职务</Radio>
                <Radio label="dep">部门</Radio>
                <Radio label="all">所有人</Radio>
            </RadioGroup>
        </div>
        <Input v-model="seartxt" size="small" placeholder="搜索人员"  @on-change="toSearch(seartxt)" v-if="partype=='all'"/>
        <Select v-model="dutie_id" v-if="partype=='dus'" size="small" @on-change="chax" placeholder="请选择职务">
            <Option :value="item.value" v-for="(item,ind) in duties" :key="ind">{{item.title}}</Option>
        </Select>
        <Cascader :data="trees" size="small" @on-change="changed" :load-data="loadData" change-on-select placeholder="请选择部门" v-if="partype=='dep'"></Cascader>
        <div style="min-height:250px;margin-top:7px;height:400px;overflow:auto">
            <RadioGroup v-model="examine_user" @on-change="toselec">
                <div v-for="item in examiners" :key="item.user_id" style="margin-bottom:2px"><Radio :label="item.user_id">{{item.teacher_name.zh?item.teacher_name.zh:item.teacher_name.mn}}</Radio></div>
            </RadioGroup>
            <div style="text-align:center" v-if="Messagem">-----{{Message}}-----</div>
            <Spin size="large" fix v-if="spinShow"></Spin>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            Message:'请选择教师',
            Messagem:true,
            spinShow:false,

            partype: 'dus',
            dutie_id: '',
            seartxt: '',
            examine_user: '',
            duties: [],
            examiners: [],
            examiners_copy: [],
            // 菜单树
            trees: []
        }
    },
    methods: {

        /************************************************************
         * 
         * 点击分类
         * 
         ************************************************************/
        ptchange(type) {
            if(type=='all') {
				this.examiners = this.examiners_copy
			} else if(type=='dus'){
				this.examiners = []
				this.dutie_id = ''
			} else if(type=='dep') {
                this.examiners = []
                if(this.trees.length==0) {
                    this.loadep()
                }
            }
        },

        /************************************************************
         * 
         * 点击职务获取教师
         * 
         ************************************************************/
        chax(id) {
            this.spinShow=true  
            this.$http.get('api/user/v1/get/teacher/user/list?duties_id=['+id+']')
            .then(val=>{  
                this.examiners=val.data
                if(this.examiners.length==0){
                    this.Message='暂无数据',
                    this.Messagem=true
                }else{
                    this.Messagem=false
                }
                this.spinShow=false          
            })
            .catch(err=>{
                if(err.code && err.code==422) {
                    this.$Message.info(this.$t('honorschooltelst'));
                }
                this.spinShow = false;
            })
        },
       
       
        /************************************************************
         * 
         * 从所有教师搜索
         * 
         ************************************************************/
		toSearch(txt) {
			if(txt.length!=0) {
				this.examiners = this.examiners_copy.filter(item=> {
					return new RegExp(txt).test(item.teacher_name)
				})
			} else {
				this.examiners = this.examiners_copy
			}
        },
        
        /************************************************************
         * 
         * 加载职务数据
         * 
         ************************************************************/
        zwload() {
            this.$http.get('api/user/v1/get/duties/list')
            .then(res=>{  
				this.duties = res.data.map(item=> ({
                    value: item.id,
                    title: item.duties_name.zh?item.duties_name.zh:item.duties_name.mn
                }))
                if(this.duties.length==0){
                    this.Message='暂无数据',
                    this.Messagem=true
                }else{
                    this.Messagem=false
                }
                this.spinShow=false          
            })
            .catch(err=>{
                if(err.code && err.code==422) {
                    this.$Message.info(this.$t('honorschooltezwlst'));
                }
                this.spinShow = false;
            })
        },
        
        /************************************************************
         * 
         * 加载所有教师数据
         * 
         ************************************************************/
        getExamine(){
            this.$http.get('api/user/v1/get/teacher/user/list')
            .then(val=>{  
				this.examiners = []
				this.examiners_copy = val.data
                if(this.examiners_copy.length==0){
                    this.Message='暂无数据',
                    this.Messagem=true
                }else{
                    this.Messagem=false
                }
                this.spinShow=false          
            })
            .catch(err=>{
                if(err.code && err.code==422) {
                    this.$Message.info(this.$t('honorschooltelst'));
                }
                this.spinShow = false;
            })
        },
        
        /************************************************************
         * 
         * 加载一级部门
         * 
         ************************************************************/
        loadep() {
            this.$http.get('api/user/v1/department/info')
            .then(res=>{  
                this.trees = this.process(res.data)         
            })
            .catch(err=>{
                if(err.code && err.code==422) {
                    this.$Message.info(this.$t('honorschooltebmlst'));
                }
            })
        },

        /************************************************************
         * 
         * 处理数据
         * 
         ************************************************************/
        process(data) {
            let arr = []
            data.forEach(item => {
                item.title = item.label = item.department_name.zh?item.department_name.zh:item.department_name.mn
                item.value = item.id
                if(item.childs) {
                    item.loading = false,
                    item.children = []
                }
                arr.push(item)
            })
            return arr
        },

        /************************************************************
         * 
         * 异步加载二级部门
         * 
         ************************************************************/
        loadData(item, callback) {
            item.loading = true;
            this.$http.get('api/user/v1/department/info?pid='+item.id)
            .then(res=>{  
                item.children = this.process(res.data)
                item.loading = false;
                callback()        
            })
            .catch(err=>{
                if(err.code && err.code==422) {
                    this.$Message.info(this.$t('honorschooltezbmlst'));
                }
            })
        },

        /************************************************************
         * 
         * 点击部门获取教师数据
         * 
         ************************************************************/
        changed(value) {
            this.spinShow=true 
            let id = value[value.length-1]
            let url = `api/user/v1/get/teacher/user/list?department_id=[${id}]`
            this.$http.get(url)
            .then(res=>{  
				this.examiners = res.data 
                if(this.examiners.length==0){
                    this.Message='暂无数据',
                    this.Messagem=true
                }else{
                    this.Messagem=false
                }
                this.spinShow=false       
            })
            .catch(err=>{
                if(err.code && err.code==422) {
                    this.$Message.info(this.$t('honorschooltelst'));
                }
                this.spinShow=false 
            })
        },

        toselec(val) {
            let str = this.examiners.filter(item => item.user_id == val)
            let tp={}
            tp.value=val
            tp.lable=str[0].teacher_name
            this.$emit('on-select',tp)
        }
    },
    created() {
        this.zwload()
        this.getExamine()
    }
}
</script>

<style>

</style>
